/*
 * Ant Group
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.mashibing.tank;

import java.awt.*;
import java.util.Random;

/**
 * 面向对象的思想
 *
 * @author zhouzheng
 * @version Tank.java, v 0.1 2021年08月20日 11:43 上午 zhouzheng
 */
public class Tank {
    private int x, y;
    private              Dir       dir    = Dir.DOWN;
    private static final int       SPEED  = 5;
    private              boolean   moving = true;
    private              TankFrame tf     = null;
    private              Random    random = new Random();
    private              Group     group  = Group.BAD;
    public static final  int       WIDTH  = ResourceMgr.goodTankU.getWidth();
    public static final  int       HEIGHT = ResourceMgr.goodTankU.getHeight();
    Rectangle rect = new Rectangle();

    public boolean isMoving() {
        return moving;
    }

    private boolean living = true;

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Tank(int x, int y, Dir dir,Group group, TankFrame tf) {
        super();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tf = tf;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }

    public void paint(Graphics g) {
        if (!living) { tf.tanks.remove(this); }
        switch (dir) {
            case LEFT:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null);
                break;
        }
        move();

    }

    private void move() {
        if (!moving) { return; }
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }


        if (this.group == Group.BAD && random.nextInt(100) > 90) {
            this.fire();
            ranDomDir();
        }

        boundsCheck();

        rect.x = this.x;
        rect.y = this.y;

    }

    private void boundsCheck() {
        if (this.x < 2 ) x = 2 ;
        else if (this.y < 28) y = 28;
        else if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH - 2) x = TankFrame.GAME_WIDTH - Tank.WIDTH -2;
        else if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT -2 ) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT -2;
    }

    private void ranDomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void fire() {
        int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int bY = this.y + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));
    }

    public void die() {
        this.living = false;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }
}